home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cpptutor.arc / CHAP08.TXT < prev    next >
Text File  |  1991-04-28  |  15KB  |  356 lines

  1.  
  2.  
  3.  
  4.                                                         Chapter 8
  5.                                                  MORE INHERITANCE
  6.  
  7. In the last chapter we developed a model using modes of
  8. transportation to illustrate the concept of inheritance.  In this
  9. chapter we will use that model to illustrate some of the finer
  10. points of inheritance and what it can be used for.  If it has been
  11. a while since you read and studied chapter 7, it would be good for
  12. you to return to that material and review it in preparation for a
  13. more detailed study of the topic of inheritance.
  14.  
  15.  
  16.  
  17. REORGANIZED FILE STRUCTURE
  18. _________________________________________________________________
  19.  
  20. A close examination of the file named            ================
  21. INHERIT1.CPP will reveal that it is identical to   INHERIT1.CPP
  22. the program developed in chapter 7 named         ================
  23. ALLVEHIC.CPP except that the program text is
  24. rearranged.  The biggest difference is that some
  25. of the simpler methods in the classes have been changed to inline
  26. code to shorten the file considerably.  In a practical programming
  27. situation, methods that are this short should be programmed inline
  28. since the actual code to return a simple value is shorter than the
  29. code required to actually send a message to a non-inline method.
  30.  
  31. The only other change is the reordering of the classes and
  32. associated methods with the classes all defined first, followed by
  33. the main program.  This puts all class interface definitions on a
  34. single page to make the code easier to study.  The implementations
  35. for the methods are deferred until the end of the file where they
  36. are available for quick reference but are not cluttering up the
  37. class definitions which we wish to study carefully in this chapter. 
  38. This should be an indication to you that there is considerable
  39. flexibility in the way the classes and methods can be arranged in
  40. C++.  Of course you realize that this violates the spirit of C++
  41. and its use of separate compilation, but is only done here for
  42. convenience.
  43.  
  44. As mentioned before, the two subclasses, car and truck, each have
  45. a variable named passenger_load which is perfectly legal, and the
  46. car class has a method of the same name, initialize(), as one
  47. defined in the super-class named vehicle.  The rearrangement of the
  48. files in no way voids this allowable repeating of names.
  49.  
  50. After you have convinced yourself that this program is truly
  51. identical to the program named ALLVEHIC.CPP from chapter 7, compile
  52. and execute it with your compiler to assure yourself that this
  53. arrangement is legal.
  54.  
  55.  
  56.  
  57.  
  58.                                                          page 8-1
  59.  
  60.                                      Chapter 8 - More Inheritance
  61.  
  62. THE SCOPE OPERATOR
  63. _________________________________________________________________
  64.  
  65. Because the method initialize() is defined in the car subclass, it
  66. hides the method of the same name which is part of the parent
  67. class, and there may be times you wish to send a message to the
  68. method in the parent class for use in the subclass object.  This
  69. can be done by using the scope operator in the following manner in
  70. the main program;
  71.  
  72.    sedan.vehicle::initialize(4,3500.0);
  73.  
  74. As you might guess, the number and types of parameters must agree
  75. with those of the method in the parent class because it will
  76. respond to the message.
  77.  
  78.  
  79. HIDDEN METHODS
  80. _________________________________________________________________
  81.  
  82. Examine the file named INHERIT2.CPP carefully    ================
  83. and you will notice that it is a repeat of the     INHERIT2.CPP
  84. last example program with a few minor changes.   ================
  85.  
  86. You will notice that the derived classes named
  87. car and truck do not have the keyword public prior to the name of
  88. the parent class in the first line of each.  The keyword public,
  89. when included prior to the parent's name, makes all of the methods
  90. defined in the parent class available for use in the derived class
  91. just as if they were defined as part of the subclass.  Therefore,
  92. in the previous program, we were permitted to call the methods
  93. defined as part of the parent class from the main program even
  94. though we were working with an object of one of the derived
  95. classes.  One example of when we did this was when we sent a
  96. message to the sedan to get its weight in an output statement of
  97. the main program.
  98. In the present program, without the keyword public prior to the
  99. parent class name, the only methods available for objects of the
  100. car class, are those that are defined as part of the class itself,
  101. and therefore we only have the methods named initialize() and
  102. passengers() available for use with objects of class car.  In this
  103. program, the only inheritance is that of variables since the two
  104. variables are inherited into objects of class car but even they are
  105. not directly available as will soon be seen.  
  106.  
  107. When we declare an object of type car, according to the definition
  108. of the C++ language, it contains three variables.  It contains the
  109. one defined as part of its class named passenger_load and the two
  110. that are part of its parent class, wheels and weight.  The only
  111. variable that is available for direct use within its methods is the
  112. one defined as part of its own class, the other two are effectively
  113. hidden from its methods.  You will note that there is no way in
  114. this program that we can ever use the variables named wheels or
  115. weight directly in either an external program or one of the methods
  116.  
  117.                                                          page 8-2
  118.  
  119.                                      Chapter 8 - More Inheritance
  120.  
  121. of this class.  The variables are a part of an object of class car
  122. when each is declared and is stored as part of the object, but the
  123. only way to use them is through use of the methods defined as part
  124. of the parent class.  They are initialized in line 86 to illustrate
  125. the means used to access them.
  126.  
  127. We will show you a way to access the parent class variables
  128. directly within local methods shortly in this chapter.  For now,
  129. we will return to the use of the subclasses in this example
  130. program.
  131.  
  132. The observant student will notice that several of the output
  133. statements have been commented out of the main program since they
  134. are no longer legal or meaningful operations.  Lines 56 through 58
  135. have been commented out because the methods named get_weight() and
  136. wheel_loading() are not inherited into the car class without the
  137. keyword public in the car class definition.  You will notice that
  138. initialize() is still available but this is the one in the car
  139. class, not the method of the same name in the vehicle class.
  140.  
  141. Moving on to the use of the truck class in the main program, we
  142. find that lines 62 and 64 are commented out for the same reason as
  143. given above, but lines 65 and 66 are commented out for an entirely
  144. different reason.  Even though the method named efficiency() is
  145. available and can be called as a part of the truck class, it cannot
  146. be used because we have no way to initialize the wheels or weight
  147. of the truck objects.  We can get the weight of the truck objects,
  148. as we have done in line 104, by using the scope resolution
  149. operator, but since the weight has no way to be initialized, the
  150. result is meaningless and lines 65 and 66 are commented out.
  151.  
  152. As you have surely guessed by now, there is a way around all of
  153. these problems and we will cover them shortly.  In the meantime,
  154. be sure to compile and execute this example program to see that
  155. your compiler gives the same result.  It would be a good exercise
  156. for you to reintroduce some of the commented out lines to see what
  157. sort of an error message your compiler issues for these errors.
  158.  
  159.  
  160. INITIALIZING ALL DATA
  161. _________________________________________________________________
  162.  
  163. If you will examine the example program named    ================
  164. INHERIT3.CPP, you will find that we have fixed     INHERIT3.CPP
  165. the initialization problem that we left dangling ================
  166. in the last example program.
  167.  
  168. The method named init_truck() now contains all four of the
  169. parameters as input data and it calls the method named initialize()
  170. of class vehicle within its implementation.  You will notice that
  171. we must call the method using the scope resolution operator in line
  172. 97 since there is no object to call, only the class.  Following the
  173. initialization, it is permissible to call the semi.efficiency()
  174. method in line 65 and 66 of the main program.
  175.  
  176.  
  177.                                                          page 8-3
  178.  
  179.                                      Chapter 8 - More Inheritance
  180.  
  181.  
  182. Be sure to compile and execute this program following your detailed
  183. study of it.
  184.  
  185.  
  186. WHAT IS PROTECTED DATA?
  187. _________________________________________________________________
  188.  
  189. Examine the program named INHERIT4.CPP for our   ================
  190. first example of the use of protected data.        INHERIT4.CPP
  191. Just to make the program more versatile, we have ================
  192. returned to the use of the keyword public prior
  193. to the name of the parent classes in lines 18
  194. and 29 of the class definitions.
  195.  
  196.  
  197. If the data within a superclass were totally available in all
  198. classes inheriting that superclass, it would be a simple matter for
  199. a programmer to inherit the superclass into a derived class and
  200. have free access to all data in the parent class.  This would
  201. completely override the protection afforded by the use of
  202. information hiding.  For this reason, the data in a class are not
  203. automatically available to the methods of an inheriting class. 
  204. There are times when you may wish to automatically inherit all
  205. variables directly into the subclasses and have them act just as
  206. though they were defined as a part of those classes also.  For this
  207. reason, the designer of C++ has provided the keyword protected.
  208.  
  209. In the present example program, the keyword protected is given in
  210. line 5 so that all of the data of the vehicle class can be directly
  211. imported into any derived classes but are not available outside of
  212. the class or derived classes.  All data are automatically defaulted
  213. to private type if no specifier is given, as in all earlier
  214. programs in this chapter.  The keyword private can be used as
  215. illustrated in lines 19 and 30 but adds nothing due to the default.
  216.  
  217. You will notice that the variables named wheels and weight are
  218. available to use in the method named initialize() in lines 85
  219. through 91 just as if they were declared as a part of the car class
  220. itself, since they are used directly.  We can now state the rules
  221. for the three means of defining variables and methods.
  222.  
  223.      private - The variables and methods are not available to any
  224.           outside calling routines, and they are not available
  225.           to any subclasses inheriting this class.
  226.  
  227.      protected - The variables and methods are not available to any
  228.           outside calling routines, but they are available to
  229.           any subclass inheriting this class.
  230.  
  231.      public - All variables and methods are freely available to all
  232.           outside calling routines and to all subclasses.
  233.  
  234.  
  235.  
  236.  
  237.                                                          page 8-4
  238.  
  239.                                      Chapter 8 - More Inheritance
  240.  
  241. You will note that these three means of definition can also be used
  242. in a struct type.  The only difference with a struct is that
  243. everything defaults to public until one of the other keywords is
  244. used.
  245.  
  246. Be sure to compile and execute this program before continuing on
  247. to the next example program.
  248.  
  249.  
  250. INHERITING CONSTRUCTORS
  251. _________________________________________________________________
  252.  
  253. Examine the example program named INHERIT5.CPP   ================
  254. for yet another variation to our basic program,    INHERIT5.CPP
  255. this time adding constructors.                   ================
  256.  
  257. The vehicle class has a constructor to
  258. initialize the number of wheels and the weight to the indicated
  259. values and has no surprising constructs.  The car and truck classes
  260. each have a constructor also to initialize their unique variables
  261. to some unique values.  If you jump ahead to the main program, you
  262. will find that the initializing statements are commented out for
  263. each of the objects so we must depend on the constructors to
  264. initialize the variables.  The most important thing to glean from
  265. this example program is the fact that when one of the constructors
  266. is called for a derived class, the constructor is also called for
  267. the parent class.  In fact, the constructor for the parent class
  268. will be called before the constructor for the derived class is
  269. called.  All of the data will be initialized, including the data
  270. inherited from the parent class.
  271.  
  272. Be sure to compile and execute this example program.
  273.  
  274.  
  275. POINTERS TO AN OBJECT AND AN ARRAY OF OBJECTS
  276. _________________________________________________________________
  277.  
  278. Examine the final example program in this        ================
  279. chapter named INHERIT6.CPP for examples of the     INHERIT6.CPP
  280. use of an array of objects and a pointer to an   ================
  281. object.
  282.  
  283. The program is identical to the first program in this chapter until
  284. we get to the main program where we find an array of 3 objects of
  285. class car declared in line 51.  It should be obvious that any
  286. operation that is legal for a simple object is legal for an object
  287. that is part of an array, but we must be sure to tell the system
  288. which object of the array we are interested in by adding the array
  289. subscript as we do in lines 55 through 61.  The operation of this
  290. portion of the program should be very easy for you to follow, so
  291. we will go on to the next construct of interest.
  292.  
  293. You will notice, in line 64, that we do not declare an object of
  294. type truck but a pointer to an object of type truck.  In order to
  295.  
  296.                                                          page 8-5
  297.  
  298.                                      Chapter 8 - More Inheritance
  299.  
  300. use the pointer, we must give it something to point at which we do
  301. in line 66 by dynamically allocating an object.  Once the pointer
  302. has an object to point to, we can use the object in the same way
  303. we would use any object, but we must use the pointer notation to
  304. access any of the methods of the object.  This is illustrated for
  305. you in lines 67 through 71, and will be further illustrated in the
  306. example programs of chapters 12 and 13 of this tutorial.
  307.  
  308. Finally, we deallocate the object in line 72.  You should spend
  309. enough time with this program to thoroughly understand the new
  310. material presented here, then compile and execute it.
  311.  
  312.  
  313. PROGRAMMING EXERCISES
  314. _________________________________________________________________
  315.  
  316.  
  317. 1.   Remove the comment delimiters from lines 65 and 66 of
  318.      INHERIT2.CPP to see what kind of results are returned.  Remove
  319.      them from line 56 to see what kind of an error is reported by
  320.      the compiler for this error.
  321.  
  322. 2.   Add cout statements to each of the constructors of
  323.      INHERIT5.CPP to output messages to the monitor so you can see
  324.      the order of sending messages to the constructors.
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.                                                          page 8-6
  356.